﻿Option Explicit On

'*******************************************************************
' Program: xmlVisio
' Author: Albert E Edlund
' Date:
'
' Purpose:
' Working with visio drawing in an xml format
'
'
'
'*******************************************************************

Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.Text
Imports System.Text.StringBuilder

Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging

Imports System.IO
Imports System.Windows.Forms
Imports System.Xml
Imports System.Xml.Serialization
Imports System.Xml.Linq
Imports System.Xml.XPath

Imports <xmlns="http://schemas.microsoft.com/visio/2003/core">
Imports <xmlns:vx="http://schemas.microsoft.com/visio/2006/extension">
Imports <xmlns:v14="http://schemas.microsoft.com/office/visio/2010/extension">





Public Class frmSectionsLocalData





#Region " Properties and Declarations "


    '
    ' where are we keeping the drawings
    '
    Private _dataPath As String
    Public Property dataPath As String
        Get
            dataPath = _dataPath
        End Get
        Set(value As String)
            _dataPath = value
        End Set
    End Property

    '
    ' what is the latest document
    '
    Public _xDoc As XDocument
    Public Property xDoc As XDocument
        Get
            Return Me._xDoc
        End Get
        Set(value As XDocument)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("Value", _
                    "xDoc is Null.")
            End If
            Me._xDoc = value
        End Set
    End Property


    Private _PgSource As XElement
    Public Property PgSource As XElement
        Get
            PgSource = _PgSource
        End Get
        Set(value As XElement)
            _PgSource = value
        End Set
    End Property


    Private _ShpSource As XElement
    Public Property ShpSource As XElement
        Get
            ShpSource = _ShpSource
        End Get
        Set(value As XElement)
            _ShpSource = value
        End Set
    End Property

    Private _SectionShapeDataEnabled As Boolean
    Public Property sectionShapeDataEnabled As Boolean
        Get
            sectionShapeDataEnabled = _SectionShapeDataEnabled
        End Get
        Set(value As Boolean)
            _SectionShapeDataEnabled = value
        End Set
    End Property

    Private _sectionUserEnabled As Boolean
    Public Property sectionUserEnabled As Boolean
        Get
            sectionUserEnabled = _sectionUserEnabled
        End Get
        Set(value As Boolean)
            _sectionUserEnabled = value
        End Set
    End Property

    Private _sectionScratchEnabled As Boolean
    Public Property sectionScratchEnabled As Boolean
        Get
            sectionScratchEnabled = _sectionScratchEnabled
        End Get
        Set(value As Boolean)
            _sectionScratchEnabled = value
        End Set
    End Property


#End Region

    '
    '
    '
    Public Sub getLocalData()



        getShapeData(ShpSource, Me.lvShapeData)
        getUserData(ShpSource, Me.lvUserData)
        getScratchData(ShpSource, Me.lvScratchData)

    End Sub

    '
    '
    '
    Private Sub getShapeData _
                (ByVal shapeSource As XElement, _
                 ByRef lvControl As ListView)

        If lvControl Is Nothing Then lvControl = Me.lvShapeData

        ' clear the control and setup the columns
        lvControl.Items.Clear()
        lvControl.Columns.Clear()
        lvControl.View = View.Details
        lvControl.AllowColumnReorder = True
        lvControl.FullRowSelect = True
        lvControl.Columns.Add("Index", 50, HorizontalAlignment.Right)
        lvControl.Columns.Add("NameU", 100, HorizontalAlignment.Left)
        lvControl.Columns.Add("Value", 100, HorizontalAlignment.Left)
        lvControl.Columns.Add("Prompt", 100, HorizontalAlignment.Left)
        lvControl.Columns.Add("Label", 100, HorizontalAlignment.Left)
        lvControl.Columns.Add("Format", 50, HorizontalAlignment.Left)
        lvControl.Columns.Add("SortKey", 100, HorizontalAlignment.Left)
        lvControl.Columns.Add("Type", 50, HorizontalAlignment.Left)
        lvControl.Columns.Add("Inv", 50, HorizontalAlignment.Left)
        lvControl.Columns.Add("Verify", 50, HorizontalAlignment.Left)
        lvControl.Columns.Add("LangId", 50, HorizontalAlignment.Left)
        lvControl.Columns.Add("Calendar", 50, HorizontalAlignment.Left)

        Dim lvItem As ListViewItem

        If sectionShapeDataEnabled = False Then Exit Sub

        ' no drill down
        Dim qryCustProp = shapeSource.<Prop>
        Dim intCustProp As Integer = qryCustProp.Count

        For Each prop In qryCustProp

            Dim intID As Integer = 0
            Dim strCpNameU As String = "NoCpNameU"
            Dim strCpValue As String = "NoCpValue"
            Dim strCpPrompt As String = "NoCpPrompt"
            Dim strCpLabel As String = "NoCpLabel"
            Dim strCpFormat As String = "NoCpFormat"
            Dim strCpSortKey As String = "NoCpSortKey"
            Dim strCpType As String = "NoCpType"
            Dim strCpInvisible As String = "NoCpInv"
            Dim strCpVerify As String = "NoCpVerify"
            Dim strCpLangId As String = "NoCpLangId"
            Dim strCpCalendar As String = "NoCpCalendar"

            ' first the attributes 
            Dim listOfAttributes As IEnumerable(Of XAttribute) = _
                    From att In prop.Attributes() _
                    Select att
            For Each att As XAttribute In listOfAttributes
                ' Console.WriteLine("{0}={1}", att.Name.LocalName, att.Value)
                Select Case LCase(att.Name.LocalName)
                    Case "id"
                        intID = CInt(att.Value)
                    Case "nameu"
                        strCpNameU = CStr(att.Value)
                End Select
            Next att

            ' now the element data
            Dim listOfElements As IEnumerable(Of XElement) = _
                From elem In prop.Elements() _
                Select elem

            If listOfElements.Count > 0 Then
                For Each elem As XElement In listOfElements
                    ' Console.WriteLine("Elem {0}={1}", elem.Name.LocalName, elem.Value)
                    Select Case LCase(elem.Name.LocalName)
                        Case "value"
                            strCpValue = CStr(elem.Value)
                            Exit Select
                        Case "prompt"
                            strCpPrompt = CStr(elem.Value)
                            Exit Select
                        Case "label"
                            strCpLabel = CStr(elem.Value)
                            Exit Select
                        Case "format"
                            strCpFormat = CStr(elem.Value)
                            Exit Select
                        Case "sortkey"
                            strCpSortKey = CStr(elem.Value)
                            Exit Select
                        Case "type"
                            strCpType = CStr(elem.Value)
                            Exit Select
                        Case "invisible"
                            strCpInvisible = CStr(elem.Value)
                            Exit Select
                        Case "verify"
                            strCpVerify = CStr(elem.Value)
                            Exit Select
                        Case "langid"
                            strCpLangId = CStr(elem.Value)
                            Exit Select
                        Case "calendar"
                            strCpCalendar = CStr(elem.Value)
                    End Select
                Next elem
            End If ' test to make sure we got at least one 

            lvItem = New ListViewItem(intID)
            lvItem.SubItems.Add(strCpNameU)
            lvItem.SubItems.Add(strCpValue)
            lvItem.SubItems.Add(strCpPrompt)
            lvItem.SubItems.Add(strCpLabel)
            lvItem.SubItems.Add(strCpFormat)
            lvItem.SubItems.Add(strCpSortKey)
            lvItem.SubItems.Add(strCpType)
            lvItem.SubItems.Add(strCpInvisible)
            lvItem.SubItems.Add(strCpVerify)
            lvItem.SubItems.Add(strCpLangId)
            lvItem.SubItems.Add(strCpCalendar)

            ' add it to the listview on the data page
            lvControl.Items.Add(lvItem)

        Next prop

    End Sub

    '
    '
    '
    Private Sub getUserData _
                (ByVal shapeSource As XElement, _
                ByRef lvControl As ListView)

        If lvControl Is Nothing Then lvControl = Me.lvUserData

        ' clear the control and setup the columns
        lvControl.Items.Clear()
        lvControl.Columns.Clear()
        lvControl.View = View.Details
        lvControl.AllowColumnReorder = True
        lvControl.FullRowSelect = True
        lvControl.Columns.Add("Index", 50, HorizontalAlignment.Right)
        lvControl.Columns.Add("NameU", 100, HorizontalAlignment.Left)
        lvControl.Columns.Add("Value", 100, HorizontalAlignment.Left)
        lvControl.Columns.Add("Prompt", 100, HorizontalAlignment.Left)


        Dim lvItem As ListViewItem

        If sectionUserEnabled = False Then Exit Sub

        Dim qryUser = shapeSource.<User>
        Dim intUser As Integer = qryUser.Count

        For Each user In qryUser

            Dim intID As Integer = 0
            Dim strUserNameU As String = "NoUserNameU"
            Dim strUserValue As String = "NoUserValue"
            Dim strUserPrompt As String = "NoUserPrompt"


            ' first the attributes 
            Dim listOfAttributes As IEnumerable(Of XAttribute) = _
                    From att In user.Attributes() _
                    Select att
            For Each att As XAttribute In listOfAttributes
                ' Console.WriteLine("{0}={1}", att.Name.LocalName, att.Value)
                Select Case LCase(att.Name.LocalName)
                    Case "id"
                        intID = CInt(att.Value)
                    Case "nameu"
                        strUserNameU = CStr(att.Value)
                End Select
            Next att

            ' now the element data
            Dim listOfElements As IEnumerable(Of XElement) = _
                From elem In user.Elements() _
                Select elem

            If listOfElements.Count > 0 Then
                For Each elem As XElement In listOfElements
                    ' Console.WriteLine("Elem {0}={1}", elem.Name.LocalName, elem.Value)
                    Select Case LCase(elem.Name.LocalName)
                        Case "value"
                            strUserValue = CStr(elem.Value)
                            Exit Select
                        Case "prompt"
                            strUserPrompt = CStr(elem.Value)
                    End Select
                Next elem
            End If ' test to make sure we got at least one 

            lvItem = New ListViewItem(intID)
            lvItem.SubItems.Add(strUserNameU)
            lvItem.SubItems.Add(strUserValue)
            lvItem.SubItems.Add(strUserPrompt)

            ' add it to the listview on the data page
            lvControl.Items.Add(lvItem)

        Next user

    End Sub

    Private Sub getScratchData _
                (ByVal shapeSource As XElement, _
                ByRef lvControl As ListView)

        If lvControl Is Nothing Then lvControl = Me.lvScratchData

        ' clear the control and setup the columns
        lvControl.Items.Clear()
        lvControl.Columns.Clear()
        lvControl.View = View.Details
        lvControl.AllowColumnReorder = True
        lvControl.FullRowSelect = True
        lvControl.Columns.Add("Index", 50, HorizontalAlignment.Right)
        lvControl.Columns.Add("X", 100, HorizontalAlignment.Left)
        lvControl.Columns.Add("Y", 100, HorizontalAlignment.Left)
        lvControl.Columns.Add("A", 100, HorizontalAlignment.Left)
        lvControl.Columns.Add("B", 100, HorizontalAlignment.Left)
        lvControl.Columns.Add("C", 100, HorizontalAlignment.Left)
        lvControl.Columns.Add("D", 100, HorizontalAlignment.Left)

        Dim lvItem As ListViewItem

        If sectionScratchEnabled = False Then Exit Sub

        Dim qryScratch = shapeSource.<Scratch>
        Dim intScratch As Integer = qryScratch.Count

        For Each scratch In qryScratch

            Dim intIX As Integer = 0
            Dim strScratchX As String = "NoUserX"
            Dim strScratchY As String = "NoUserY"
            Dim strScratchA As String = "NoUserA"
            Dim strScratchB As String = "NoUserB"
            Dim strScratchC As String = "NoUserC"
            Dim strScratchD As String = "NoUserD"


            ' first the attributes 
            Dim listOfAttributes As IEnumerable(Of XAttribute) = _
                    From att In scratch.Attributes() _
                    Select att
            For Each att As XAttribute In listOfAttributes
                ' Console.WriteLine("{0}={1}", att.Name.LocalName, att.Value)
                Select Case LCase(att.Name.LocalName)
                    Case "ix"
                        intIX = CInt(att.Value)
                End Select
            Next att

            ' now the element data
            Dim listOfElements As IEnumerable(Of XElement) = _
                From elem In scratch.Elements() _
                Select elem

            If listOfElements.Count > 0 Then
                For Each elem As XElement In listOfElements
                    ' Console.WriteLine("Elem {0}={1}", elem.Name.LocalName, elem.Value)
                    Select Case LCase(elem.Name.LocalName)

                        Case "x"
                            strScratchX = CStr(elem.Value)
                            Exit Select
                        Case "y"
                            strScratchY = CStr(elem.Value)
                            Exit Select
                        Case "a"
                            strScratchA = CStr(elem.Value)
                            Exit Select
                        Case "b"
                            strScratchB = CStr(elem.Value)
                            Exit Select
                        Case "c"
                            strScratchC = CStr(elem.Value)
                            Exit Select
                        Case "d"
                            strScratchD = CStr(elem.Value)
                    End Select
                Next elem
            End If ' test to make sure we got at least one 

            lvItem = New ListViewItem(intIX)
            lvItem.SubItems.Add(strScratchX)
            lvItem.SubItems.Add(strScratchY)
            lvItem.SubItems.Add(strScratchA)
            lvItem.SubItems.Add(strScratchB)
            lvItem.SubItems.Add(strScratchC)
            lvItem.SubItems.Add(strScratchD)

            ' add it to the listview on the data page
            lvControl.Items.Add(lvItem)

        Next scratch



    End Sub

    '
    '
    '
    Private Sub ExitToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    '
    '
    '
    Private Sub AboutToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        frmAbout.ShowDialog()
    End Sub

End Class